<?php 

define('ROOT', dirname( __FILE__ ).'/' );
define('MODULES', ROOT.'modules/');
define('TPL', ROOT.'templates/');
define('PUB', 'public/');
define('LIB', 'libs/');
 

require_once( LIB.'mysql.php');
require_once( LIB.'functions.php');
require_once( LIB.'FirePHPCore/fb.php');

class Controller
{
	private static $instance = null;
	
	public static $configs = null;
	
	public static $input = null;
	
	public static $error = array();
	
	public static $message = array();
	
	private $output = null;
	
	private $request = null;
	
	public static function instance()
	{
		if( !self::$instance )
		{
			self::loadConfigs();
			self::parseInput();
			self::$instance = new self();
			self::$instance->parseURI();
		}
		
		return self::$instance;
	}
	
	private static function loadConfigs()
	{
		self::$configs = include('config.php');
	}
	
	private static function parseInput()
	{
		self::$input = $_REQUEST;
	}
	
	private function parseURI()
	{
		$request = array();
		$request['module'] = isset( Controller::$input['module'] ) ? Controller::$input['module'] : 'home';
		$request['action'] = isset( Controller::$input['act'] ) ? Controller::$input['act'] : 'process';
		$this->request = $request;
	}
	
	
	public static function runModule( $ModuleName='home', $Action = 'process' )
	{
		$modulePath = MODULES.$ModuleName.'/index.php';
		if( file_exists( $modulePath ) )
		{
			require_once $modulePath;
			$moduleClass = ucfirst(strtolower($ModuleName));
			$module = new $moduleClass();
			
			if( method_exists( $module, $Action ) )
			{
				$result = $module->{$Action}();
				return $result;
			}
			
			return array('error' => 'Action '.$Action. ' does not exists' ) ;
		}
		return array( 'error' => 'Module '.$modulePath.' does not exists' ) ;
	}
	
	public static function loadTemplate( $Data, $ModuleName = null, $Action = null )
	{
		if( $ModuleName != null )
		{
			$template = ( $Action == null ) ? 'index' : strtolower( $Action );
			$template = ( is_array( $Data ) && isset( $Data['template'] ) ) ? $Data['template'] : $template;
			$templatePath = TPL.strtolower( $ModuleName ).'/'.$template.'.php';
		}
		else
		{
			$templatePath = TPL.'layout.php';
		}
		
		if( file_exists( $templatePath ) )
		{
			ob_start();
			include( $templatePath );
			$moduleOutput = ob_get_clean();
			return array('output' => $moduleOutput ) ;
		}
		return array('output' => 'Template '.$templatePath.' does not exists.');
	}
	
	public function run()
	{
		
		$currentActionResult = Controller::runModule( $this->request['module'], $this->request['action'] ); 
		if( is_array( $currentActionResult ) && isset( $currentActionResult['error']))
		{
			echo( $currentActionResult['error'] ); exit;
		}
		
		$ModuleResult = Controller::loadTemplate( $currentActionResult, $this->request['module'], isset( $this->request['action']) ? $this->request['action'] : null );
		
		$LayoutResult = Controller::loadTemplate(array($this->request['module'] => $ModuleResult['output'] ) , null ) ;
		
		$this->output = $LayoutResult['output'];
		
		$this->output = ( $this->output == null ) ? 'Error orcurr! no output' : $this->output;
		$this->display();
		$this->end();
	}
	
	public function display()
	{
		echo ( $this->output );
		exit;
	}
	
	public static  function end()
	{
		
	}
}

session_start();
Controller::instance()->run();


?>
